package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;


@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    CourseContentService contentService = new CourseContentServiceImpl();

    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response) {

        try {
            //1.获取参数
            String course_id = request.getParameter("course_id");

            //2.业务处理

            List<Course_Section> sectionList = contentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));

            //返回结果

            String result = JSON.toJSONString(sectionList);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //根据courseId课程信息回显
    public void findCourseById(HttpServletRequest request, HttpServletResponse response) {

        try {
            //获取参数
            String course_id = request.getParameter("course_id");

            //业务处理
            Course course = contentService.findCourseByCourseId(Integer.parseInt(course_id));

            //返回数据
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name");

            String result = JSON.toJSONString(course, filter);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 保存或修改 章节信息
     *
     * @param request
     * @param response
     */
    public void saveOrUpdateSection(HttpServletRequest request, HttpServletResponse response) {

        try {
            //获取参数
            Map<String, Object> map = (Map) request.getAttribute("map");

            //创建Course_Section
            Course_Section section = new Course_Section();
            //3.使用BeanUtils工具类,将map中的数据封装到 section
            BeanUtils.populate(section, map);

            if (section.getId() != 0) {
                //修改操作
                String result = contentService.updateSection(section);
                //5.返回结果数据
                response.getWriter().println(result);
            } else {
                //5.响应结果
                String result = contentService.saveSection(section);
                response.getWriter().print(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response) {
        try {
            //1.获取参数
            int id = Integer.parseInt(request.getParameter("id"));
            int status = Integer.parseInt(request.getParameter("status"));
            //4.业务处理
            String result = contentService.updateSectionStatus(id, status);
            //5.返回结果数据findSectionById
            response.getWriter().println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存或修改 课时信息
     *
     * @param request
     * @param response
     */
    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response) {


        try {
            //获取参数
            Map<String, Object> map = (Map) request.getAttribute("map");

            //创建Course_Lesson
            Course_Lesson lesson = new Course_Lesson();
            //3.使用BeanUtils工具类,将map中的数据封装到 lesson
            BeanUtils.populate(lesson, map);

            if (lesson.getId() != 0) {
                //修改操作
                String result = contentService.updateLesson(lesson);
                //5.返回结果数据
                response.getWriter().println(result);
            } else {
                //5.响应结果
                String result = contentService.saveLesson(lesson);
                response.getWriter().print(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
